package club.banyuan.hrms.servlet;

import club.banyuan.hrms.dao.PositionDao;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class PositionDeleteServlet extends HttpServlet {
    private PositionDao positionDao;

    @Autowired
    public PositionDeleteServlet(PositionDao positionDao) {
        this.positionDao = positionDao;
    }

    public PositionDao getPositionDao() {
        return positionDao;
    }

    public void setPositionDao(PositionDao positionDao) {
        this.positionDao = positionDao;
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        String data = request.getParameter("ids");
        if (data == null) {
            return;
        }
        String[] ids = data.split(",");
        Integer[] integerIds = new Integer[ids.length];
        for (int i = 0; i < ids.length; i++) {
            integerIds[i] = Integer.parseInt(ids[i]);
        }

        if (integerIds.length > 0) {
            positionDao.deletePosition(integerIds);
        }


        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("massage", "删除成功");
        response.getWriter().println(JSONObject.toJSONString(resultMap));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
